package com.lyshop.jinding.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.lyshop.jinding.R;
import com.zc.mychart.utils.ArithUtil;
import com.zc.mychart.utils.FormatUtil;


public abstract class MySeekBar extends LinearLayout implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
    protected TextView tv_value;
    protected TextView tv_change_remark;
    protected TextView tv_min;
    protected TextView tv_max;
    protected TextView tv_describe;
    protected View bt_sub;
    protected View bt_add;
    protected View mView;
    protected SeekBar sb_progress;
    protected Context mContext;
    protected double mSingleProfit = 1;
    protected double mAmount = 1;
    protected double mUnitPrice = 1;
    protected int minValue = 0;
    protected int maxValue = 0;
    private boolean isFormatZero = false;//progress为0时是否格式化

    public MySeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mView = setContentView();
        tv_value = (TextView) mView.findViewById(R.id.tv_value);
        tv_describe = (TextView) mView.findViewById(R.id.tv_describe);
        tv_change_remark = (TextView) mView.findViewById(R.id.tv_change_remark);
        tv_min = (TextView) mView.findViewById(R.id.tv_min);
        tv_max = (TextView) mView.findViewById(R.id.tv_max);
        sb_progress = (SeekBar) mView.findViewById(R.id.sb_progress);
        bt_sub = mView.findViewById(R.id.bt_sub);
        bt_add = mView.findViewById(R.id.bt_add);
        bt_add.setOnClickListener(this);
        bt_sub.setOnClickListener(this);
        sb_progress.setOnSeekBarChangeListener(this);
        TypedArray arr = mContext.obtainStyledAttributes(attrs, R.styleable.MySeeekBar);
        boolean isHide = arr.getBoolean(R.styleable.MySeeekBar_isHide, false);
        if (isHide) {
            tv_max.setVisibility(GONE);
            tv_min.setVisibility(GONE);
        }
        arr.recycle();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_add:
                if (sb_progress.getProgress() < sb_progress.getMax()) {
                    sb_progress.setProgress(sb_progress.getProgress() + 1);
                }
                break;
            case R.id.bt_sub:
                if (sb_progress.getProgress() > 0) {
                    sb_progress.setProgress(sb_progress.getProgress() - 1);
                }
                break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        calculateProfitAndLoss();
    }

    public void calculateProfitAndLoss() {
        if (getProgress() == 0 && isFormatZero) {
            tv_value.setText("不限");
            tv_change_remark.setVisibility(GONE);
        } else {
            int value = getProgress() + minValue;
            double centDot = ArithUtil.div(mUnitPrice, mSingleProfit);//100%点数
            double div = ArithUtil.mul(ArithUtil.round(ArithUtil.div(value, centDot), 4), 100);
            tv_value.setText(FormatUtil.formatDouble2(div) + "%");
            tv_change_remark.setVisibility(VISIBLE);
            tv_change_remark.setText("(" + value + "点 " + FormatUtil.formatDouble2(ArithUtil.mul(value, mSingleProfit * mAmount)) + "元)");
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        bt_sub.setEnabled(false);
        bt_add.setEnabled(false);
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        bt_add.setEnabled(true);
        bt_sub.setEnabled(true);
    }

    public void setMaxMinValue(int max, int min, boolean isFormatZero) {
        this.minValue = min < 0 ? 0 : min;
        this.maxValue = max < 0 ? 0 : max;
        this.isFormatZero = isFormatZero;
        sb_progress.setMax(max - minValue);
        tv_max.setText(max + "点");
        if (!isFormatZero) {
            tv_min.setText(min + "点");
        } else {
            tv_min.setText("不限");
        }
    }

    public void setTitleColor(int color){
        tv_describe.setTextColor(color);
    }

    public void setValue(int value) {
        sb_progress.setProgress(value - minValue);
    }

    public int getValue() {
        if (sb_progress.getProgress() == 0 && isFormatZero) {
            return 0;
        } else {
            return sb_progress.getProgress() + minValue;
        }

    }

    public int getProgress() {
        return sb_progress.getProgress();
    }

    public void setSingleProfit(double mSingleProfit) {
        if (mSingleProfit != 0) {
            this.mSingleProfit = mSingleProfit;
        }
    }

    public void setAmount(int amout) {
        this.mAmount = amout;
    }

    public void setUnitPrice(double unitPrice) {
        this.mUnitPrice = unitPrice;
    }

    protected abstract View setContentView();

}
